import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/routers/routers.dart';
import '../config/config.dart';

///
/// dialog管理器
///
class DialogManager {
  ///===========================================================默认样式的dialog
  static showDefaultDialog(BuildContext context, String desc, Function() onConfirm,
      {String title = '提示', String okText = '确定', String canText = '取消'}) {
    AwesomeDialog(
      context: context,
      dialogType: DialogType.NO_HEADER,
      // borderSide: const BorderSide(
      //   color: Config.colorBg,
      //   width: 1,
      // ),
      width: 660.w,
      buttonsBorderRadius: BorderRadius.all(
        Radius.circular(10.w),
      ),
      dismissOnTouchOutside: false,
      dismissOnBackKeyPress: true,
      onDissmissCallback: (type) {},
      headerAnimationLoop: false,
      animType: AnimType.SCALE,
      title: title,
      desc: desc,
      showCloseIcon: false,
      btnOkText: okText,
      btnCancelText: canText,
      btnCancelColor: const Color(0xFFCCCCCC),
      btnOkColor: Config.colorMain,
      btnCancelOnPress: () {},
      btnOkOnPress: onConfirm,
    ).show();
  }

  ///===========================================================一个通用的底部弹出列表选项

  //返回商品列表item
  static getListItem(int index, String str, Function func) {
    return InkWell(
      onTap: () {
        func(index);
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(color: Config.colorDivider, width: 2.w),
          ),
        ),
        width: double.infinity,
        height: 90.w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              str,
              softWrap: false,
              style: TextStyle(
                  color: index == currentBottomMenu ? Config.colorMain : Config.colorText1,
                  fontSize: 26.sp),
            ),
          ],
        ),
      ),
    );
  }

  static int currentBottomMenu = 0;

  static showBottomMenu(BuildContext context, List<String> datas, Function func) {
    return showModalBottomSheet(
      isScrollControlled: false,
      context: context,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return Container(
          width: double.infinity,
          alignment: Alignment.center,
          height: datas.length <= 4 ? (datas.length * 90.w + 60) : (4 * 90.w + 60),
          padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(15.w),
              topRight: Radius.circular(15.w),
            ),
          ),
          child: ListView.builder(
            padding: EdgeInsets.symmetric(horizontal: 0.w),
            itemCount: datas.length,
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            physics: const BouncingScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return getListItem(index, datas[index], (index) {
                currentBottomMenu = index;
                // print(currentBottomMenu);
                func(currentBottomMenu);
                Routes.router.pop(context);
              });
            },
          ),
        );
      },
    );
  }

  //===============================================================================
  static void appUpdateDialog(
      BuildContext context, String title, String content, Function() fun) async {
    AwesomeDialog(
      context: context,
      // dialogType: DialogType.NO_HEADER,
      width: 660.w,
      buttonsBorderRadius: BorderRadius.all(
        Radius.circular(10.w),
      ),
      dismissOnTouchOutside: false,
      dismissOnBackKeyPress: false,
      onDissmissCallback: (type) {},
      headerAnimationLoop: false,
      animType: AnimType.SCALE,
      // title: title,
      // desc: content,
      showCloseIcon: false,
      customHeader: Container(
        width: 210.w,
        height: 210.w,
        padding: EdgeInsets.all(0.w),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(105),
          child: CachedNetworkImage(
            imageUrl: '',
            fit: BoxFit.fill,
            width: double.infinity,
            height: double.infinity,
            errorWidget: (context, url, error) {
              return Image.asset(
                'assets/images/icon_app_update.png',
                width: double.infinity,
                height: double.infinity,
                fit: BoxFit.fill,
              );
            },
          ),
        ),
      ),
      body: Container(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(top: 10.w),
            ),
            Text(
              title,
              style: TextStyle(fontSize: 28.sp, color: Config.colorText1),
            ),
            Padding(
              padding: EdgeInsets.only(top: 20.w),
            ),
            Text(
              content,
              style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
            ),
            Padding(
              padding: EdgeInsets.only(top: 0.w),
            ),
            Container(
              width: 360.w,
              height: 80.w,
              padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 8.w),
              margin: EdgeInsets.only(top: 45.w),
              child: ElevatedButton(
                clipBehavior: Clip.antiAlias,
                onPressed: fun,
                style: ElevatedButton.styleFrom(shape: const StadiumBorder()),
                child: Text(
                  '立即升级',
                  style: TextStyle(fontSize: 26.sp),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 25.w),
            ),
          ],
        ),
      ),
      // btnOkText: okText,
      // btnCancelText: canText,
      // btnCancelColor: const Color(0xFFDDDDDD),
      // btnOkColor: Config.colorMain,
      // btnCancelOnPress: () {},
      // btnOkOnPress: onConfirm,
    ).show();
  }
}
